﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using GosWeb.Models.Form;


namespace GosWeb.Models.Db
{
    /// <summary>
    /// Biont Management
    /// </summary>
    public class GosDBContext : DbContext
    {
        static GosDBContext ins;
        public DbSet<OtherParameter> Parameters { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Gene> Genes { get; set; }
        public DbSet<Illness> Illnesses { get; set; }
        public DbSet<GeneRatio> GeneRatios { get; set; }
        public DbSet<MedicalRecord> MedicalRecords { get; set; }
        public DbSet<Biont> Bionts { get; set; }

        public static GosDBContext GetInstance()
        {
            if (ins == null)
            {
                ins = new GosDBContext();
            }
            return ins;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class GosInitializer : DropCreateDatabaseIfModelChanges<GosDBContext>
    {
        protected override void Seed(GosDBContext context)
        {
            //var users = new List<User> { 
            //     new User {   UserName = "admin",  Password = "123456", ConfirmPassword = "123456", Email="fandongping@gmail.com" }
             
            // };
            //users.ForEach(d => context.Users.Add(d));

            var paras = new List<OtherParameter> { 
                new OtherParameter {  Code= "WB", Value="外部猪场", ParaType =(Int16) ParamType.Site },
                 new OtherParameter {  Code= "XS", Value="雪山", ParaType =(Int16) ParamType.Site },
                 new OtherParameter {  Code= "YB", Value="月背山", ParaType = (Int16) ParamType.Site},
                 new OtherParameter {  Code= "YB", Value="月背山猪场", ParaType = (Int16) ParamType.CompanName},
                 new OtherParameter {  Code= "XS", Value="雪山猪场", ParaType = (Int16) ParamType.CompanName},
            };
            paras.ForEach(d => context.Parameters.Add(d));


            var genes = new List<Gene> { 
                 new Gene {  Code= "L", Name="长白", Disable = false },
                 new Gene {  Code= "Y", Name="大白", Disable = false },
                 new Gene {  Code= "D", Name="杜洛克", Disable = false},
            };
            genes.ForEach(d => context.Genes.Add(d));

            var illnesses = new List<Illness> 
            {
                new Illness {  Code= "SQ", Name="疝气", Disable = false },
                new Illness {  Code= "KDY", Name="口蹄疫", Disable = false},
                new Illness {  Code= "WZ", Name="未知疾病", Disable = false},
                new Illness {  Code= "QT", Name="其他", Disable = false},
            };
            illnesses.ForEach(d => context.Illnesses.Add(d));

            var gr1 = new List<GeneRatio> { 
             new GeneRatio() { AnID = "T1", GeneCode = "D", Ratio = 100M }
            };

            var gr2 = new List<GeneRatio> { 
             new GeneRatio() { AnID = "T2", GeneCode = "L", Ratio = 100M }
            };

            Location lo1 = new Location() { Site = "XS", Building = 0, Room = 0 };
            Location lo2 = new Location() { Site = "YB", Building = 0, Room = 0 };

            var pps = new List<Biont> { 
                 new Biont { BiontID = "T1", Sex = 1, FromTo=(int) FromToType.OutSide, Birthday=DateTime.Parse("2011-1-15"), Address = lo1,  GeneRatios = gr1, NestNumber=1 },
                 new Biont { BiontID = "T2", Sex = 0, FromTo=(int) FromToType.ForSeed, Birthday=DateTime.Parse("2010-1-13") , Address= lo2, GeneRatios = gr2, NestNumber=2  },
             };

            pps.ForEach(d => context.Bionts.Add(d));

            //FatherMother fm1= new FatherMother {FatherID ="T2",MotherID ="T1"};
            //FatherMother fm2= new FatherMother {FatherID ="T2",MotherID ="T3"};

            var pns = new List<Biont> { 
                 new Biont { BiontID = "T3", Sex = 1, FromTo=(int) FromToType.ForSeed, Birthday=DateTime.Parse("2011-1-15"), Address = lo1, FatherID ="T2", MotherID ="T1", NestNumber=3  },
                 new Biont { BiontID = "T4", Sex = 0, FromTo=(int) FromToType.Undefined, Birthday=DateTime.Parse("2010-1-13") , Address=lo2, FatherID ="T2",MotherID ="T3", NestNumber =4},
             };
            pns.ForEach(d => context.Bionts.Add(d));
        }
    }
}